
package com.tools.cleanmaster.utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Locale;

/**
 *
 */
public class JsonUtils {

    public static String readFromStream(InputStream is) {
        if (is == null) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            return builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            if (e instanceof SocketException
                    || e instanceof SocketTimeoutException
                    || e instanceof UnknownHostException) {
            }
        } finally {
            FileHelper.close(reader);
        }
        return null;
    }

    public static boolean isNewDataAvailable(final String jsonContent) {
        if (jsonContent == null || "".equals(jsonContent)) {
            return false;
        }

        try {
            return new JSONObject(jsonContent).getJSONObject("responseHeader").getInt("status") == 200; //"status" for 3Y service
        } catch (Exception re) {
            try {
                return new JSONObject(jsonContent).getJSONObject("responseHeader").getInt("errcode") == 200;  //"errcode" for recomm service
            } catch (JSONException e) {
                re.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static boolean isValidResponseContent(final String jsonContent) {
        if (jsonContent == null || "".equals(jsonContent)) {
            return false;
        }

        try {
            return new JSONObject(jsonContent).getJSONObject("responseHeader").getInt("status") == 200;
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (RuntimeException re) {
            re.printStackTrace();
        }

        return false;
    }

    public static boolean is304Response(final String jsonContent) {
        if (jsonContent == null || jsonContent.trim().length() == 0) {
            return false;
        }
        try {
            return new JSONObject(jsonContent).getJSONObject("responseHeader").getInt("status") == 304;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean isSessionTimeOut(final String jsonContent) {
        if (jsonContent == null || "".equals(jsonContent)) {
            return false;
        }

        try {
            if (new JSONObject(jsonContent).getJSONObject("responseHeader").getString("status").equals("ok")) {
                return false;
            } else {
                return new JSONObject(jsonContent).getJSONObject("responseHeader").getString("status").equals("403");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (RuntimeException re) {
            re.printStackTrace();
        }

        return false;
    }

    public static long getJsonContentTimestamp(final String jsonContent) {
        if (jsonContent == null || "".equals(jsonContent)) {
            return -1L;
        }

        try {
            return new JSONObject(jsonContent).getJSONObject("responseHeader").getLong("lastModified");
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (RuntimeException re) {
            re.printStackTrace();
        }

        return -1L;
    }

    public static String getSession(final String jsonContent) {
        if (jsonContent == null || "".equals(jsonContent)) {
            return null;
        }

        try {
            return new JSONObject(jsonContent).getJSONObject("response").getString("session");
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (RuntimeException re) {
            re.printStackTrace();
        }

        return null;
    }

    public static String getSID(final String jsonContent) {
        if (jsonContent == null || "".equals(jsonContent)) {
            return null;
        }

        try {
            return new JSONObject(jsonContent).getString("sid");
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (RuntimeException re) {
            re.printStackTrace();
        }

        return null;
    }

    public static String decodeJsonLanguageString(String json) {
        if (json == null || !json.startsWith("{"))
            return json;
        String str = json;
        JSONObject jsonObj = null;
        try {
            jsonObj = new JSONObject(json);
            str = jsonObj.getString(getLanguage());
            return str;
        } catch (Exception e) {
            try {
                jsonObj = new JSONObject(json);
                if ("zh_TW".equals(getLanguage()) || "zh_CN".equals(getLanguage())) {
                    str = jsonObj.getString("zh");
                } else {
                    str = jsonObj.getString("en");
                }
                return str;
            } catch (Exception e2) {
                return str;
            }
        }
    }

    public static String getLanguage() {
        return Locale.getDefault().getLanguage();
    }
}
